package com.nuoniu.sibanyun.mapper.finance;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.entity.finance.VAccountingLedger;
import com.nuoniu.sibanyun.entity.finance.VAccountingSearchProgramme;
import com.nuoniu.sibanyun.entity.vo.VAccountingLedgerVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 科目总账 Mapper 接口
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-25
 */
@Mapper
@Component
public interface VAccountingLedgerMapper extends BaseMapper<VAccountingLedger> {

    @Select("select * from fag_acc_ledger where accounting_id=#{accountingId} and account_year=#{accountYear} and account_period=#{accountPeriod}")
    VAccountingLedger getById(Integer accountingId, Integer accountYear, Integer accountPeriod);

    @Select("select * from fag_acc_ledger where accounting_id in (${parentIds})")
    List<VAccountingLedger> getByParentId(String parentIds);

    @Select("select * from fag_acc_ledger where account_id=#{accountId} and ")
    VAccountingLedger getDetail(Integer accountId);

    @Select("select count(1) from fag_acc_ledger where accounting_id=#{id}")
    Integer getByAccountingId(Long id);

    /**
     * 获取上一期间科目总账
     * @author 小啊刚
     * @date 2021-04-30
     */
    @Select("select * from fag_acc_ledger where accounting_id=#{accountingId} and account_year=#{accountYear} and account_period<#{accountPeriod} order by account_period desc limit 1")
    VAccountingLedger getUpLedger(Integer accountingId, Integer accountYear, Integer accountPeriod);

    /**
     * 获取后面期间列表
     * @author 小啊刚
     * @date 2021-04-30
     */
    @Select("select * from fag_acc_ledger where accounting_id=#{accountingId} and account_year=#{accountYear} and account_period>#{accountPeriod} order by account_period")
    List<VAccountingLedger> getNextPeriod(VAccountingLedger accountingLedger);

    /**
     * 获取期初列表
     * @author 小啊刚
     * @date 2022-04-29
     */
    List<VAccountingLedgerVo> getList(@Param("companyId") Integer companyId, @Param("accountYear") Integer accountYear, @Param("accountPeriod") Integer accountPeriod, @Param("accountingTypeId") Integer accountingTypeId, @Param("name") String name, @Param("code") String code);

    @Select("select * from fag_acc_ledger where accounting_id=#{accountingId} and account_year=#{currentYear} and account_period=#{period}")
    VAccountingLedger getByAccounting(Integer accountingId, Integer currentYear, int period);

    @Select("select count(1) from fag_voucher where company_id=#{companyId} and status=3 and account_year=#{currentYear}")
    Integer checkAccounting(Integer companyId, Integer currentYear);

    Page<VAccountingLedger> pageEndAccountingList(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    Page<VAccountingLedger> getGeneralLedgerList(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    @Select("select * from fag_acc_ledger where is_end_node=1 and company_id=#{companyId} and account_year=#{accountYear} and account_period=1")
    List<VAccountingLedger> getBeginList(Integer companyId, Integer accountYear);

    List<VAccountingLedger> getFinaBalance(Map param);

    List<VAccountingLedger> getYearBalance(Map param);

    /**
     * 获取期初金额
     * @author 小啊刚
     * @date 2022-05-06
     */
    @Select("select sum(begin_amount) from fag_acc_ledger where company_id=#{companyId} and account_id=#{id} and begin_amount_direction=#{direction} and begin_amount is not null and accounting_parent_id=0")
    Double getBeginDebitAmount(Long id, Integer companyId, int direction);

    /**
     *  获取财务启用期间
     * @author 小啊刚
     * @date 2022-05-16
     */
    @Select("select account_period from fag_acc_ledger where account_year=#{year} and company_id=#{companyId} order by account_period limit 1")
    Integer getAccountPeriod(Integer year, Integer companyId);

    /**
     * 根据当月所有总账
     * @author 小啊刚
     * @date 2022-05-31
     */
    @Select("select * from fag_acc_ledger  where account_year=#{year} and company_id=#{companyId} and account_period=#{month}")
    List<VAccountingLedger> getYearLedger(int year, int month, Integer companyId);
}
